package org.herrdommel.webpagechecker.util;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import org.herrdommel.webpagechecker.util.sqlentities.Page;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class WPCDataSource {

	// Database fields
	private SQLiteDatabase database;
	private final WPCSQLiteHelper dbHelper;
	private final String[] allColumns = {WPCSQLiteHelper.COLUMN_ID, WPCSQLiteHelper.COLUMN_PAGE,
			WPCSQLiteHelper.COLUMN_RESULT, WPCSQLiteHelper.COLUMN_CACHETIME};

	public WPCDataSource(final Context context) {
		dbHelper = new WPCSQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public Page createPage(final String page, final String result) {
		final GregorianCalendar now = new GregorianCalendar();
		final ContentValues values = new ContentValues();
		values.put(WPCSQLiteHelper.COLUMN_PAGE, page);
		values.put(WPCSQLiteHelper.COLUMN_RESULT, result);
		values.put(WPCSQLiteHelper.COLUMN_CACHETIME, String.valueOf(now.getTimeInMillis()));
		final long insertId = database.insert(WPCSQLiteHelper.TABLE_PAGES, null, values);
		final Cursor cursor = database.query(WPCSQLiteHelper.TABLE_PAGES, allColumns, WPCSQLiteHelper.COLUMN_ID + "='"
				+ insertId + "'", null, null, null, null);
		cursor.moveToFirst();
		final Page newPage = cursorToPage(cursor);
		cursor.close();
		return newPage;
	}

	public void deletePage(final String page) {
		Log.d(WPCDataSource.class.getName(), "Page deleted with name: " + page);
		database.delete(WPCSQLiteHelper.TABLE_PAGES, WPCSQLiteHelper.COLUMN_PAGE + "='" + page + "'", null);
	}

	public Page getPage(final String page) {
		final Cursor cursor = database.query(WPCSQLiteHelper.TABLE_PAGES, allColumns, WPCSQLiteHelper.COLUMN_PAGE
				+ "='" + page + "'", null, null, null, null);
		if (!cursor.moveToFirst()) {
			cursor.close();
			return null;
		}
		final Page newPage = cursorToPage(cursor);
		cursor.close();
		return newPage;
	}

	public int updatePage(final String page, final String result) {
		final Cursor cursor = database.query(WPCSQLiteHelper.TABLE_PAGES, allColumns, WPCSQLiteHelper.COLUMN_PAGE
				+ "='" + page + "'", null, null, null, null);
		if (!cursor.moveToFirst()) {
			cursor.close();
			createPage(page, result);
			return 0;
		}
		final Page res = cursorToPage(cursor);
		cursor.close();

		final GregorianCalendar now = new GregorianCalendar();
		final ContentValues values = new ContentValues();
		values.put(WPCSQLiteHelper.COLUMN_ID, res.getId());
		values.put(WPCSQLiteHelper.COLUMN_PAGE, page);
		values.put(WPCSQLiteHelper.COLUMN_RESULT, result);
		values.put(WPCSQLiteHelper.COLUMN_CACHETIME, String.valueOf(now.getTimeInMillis()));
		return database.update(WPCSQLiteHelper.TABLE_PAGES, values, WPCSQLiteHelper.COLUMN_ID + "='" + res.getId()
				+ "'", null);
	}

	public List<Page> getAllPages() {
		final List<Page> pages = new ArrayList<Page>();

		final Cursor cursor = database.query(WPCSQLiteHelper.TABLE_PAGES, allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			final Page page = cursorToPage(cursor);
			pages.add(page);
			cursor.moveToNext();
		}
		cursor.close();
		return pages;
	}

	private Page cursorToPage(final Cursor cursor) {
		final Page page = new Page();
		page.setId(cursor.getLong(cursor.getColumnIndex(WPCSQLiteHelper.COLUMN_ID)));
		page.setName(cursor.getString(cursor.getColumnIndex(WPCSQLiteHelper.COLUMN_PAGE)));
		page.setResult(cursor.getString(cursor.getColumnIndex(WPCSQLiteHelper.COLUMN_RESULT)));
		page.setCachetime(cursor.getString(cursor.getColumnIndex(WPCSQLiteHelper.COLUMN_CACHETIME)));
		return page;
	}
}
